package com.example.lenovo.a203_yhy_ordering_3.activity;

import android.app.Person;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lenovo.a203_yhy_ordering_3.R;
import com.example.lenovo.a203_yhy_ordering_3.beans.UpdateUserBean;
import com.example.lenovo.a203_yhy_ordering_3.beans.UserInfoBean;
import com.example.lenovo.a203_yhy_ordering_3.fragments.MeFragment;
import com.example.lenovo.a203_yhy_ordering_3.listener.UpdateUserListener;
import com.example.lenovo.a203_yhy_ordering_3.listener.UserInfoListener;
import com.example.lenovo.a203_yhy_ordering_3.model.RegisterModel;
import com.example.lenovo.a203_yhy_ordering_3.model.UpdateUserModel;
import com.example.lenovo.a203_yhy_ordering_3.model.UserInfoModel;

public class UpdateUserActivity extends AppCompatActivity{
    private EditText te_name,te_pass,te_phone,te_address;
    private Button bu_xiugai;
    private ImageView im_return;
    private String username;
    private String password;
    private String mobilenum;
    private String address;
    private int user_id;
    private SharedPreferences sharedPreferences,sharedPreferences1;
    private String userpass;

    UpdateUserListener updateUserListener = new UpdateUserListener() {
        @Override
        public void onResponse(UpdateUserBean updateUserBean) {
            if(updateUserBean.getSuccess().equals("0")) {
                Toast.makeText(UpdateUserActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                finish();
            }else{
                Toast.makeText(UpdateUserActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                if(userpass.equals(password)){     //如果新密码与原密码相同，跳转到主界面；否则跳转到登录界面
                    Intent intent=new Intent(UpdateUserActivity.this,MainActivity.class);
                    startActivity(intent);
                }
                else {
                    Intent intent=new Intent(UpdateUserActivity.this,LoginActivity.class);
                    startActivity(intent);
                }
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(UpdateUserActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_user);

        username=getIntent().getStringExtra("username");
        password=getIntent().getStringExtra("password");
        mobilenum=getIntent().getStringExtra("phone");
        address=getIntent().getStringExtra("address");

        sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
        user_id=Integer.parseInt(sharedPreferences.getString("userid","获取userid失败"));
        sharedPreferences1=getSharedPreferences("userpass",MODE_PRIVATE);
        password=sharedPreferences1.getString("userpass","获取userpasss失败");  //获取原密码

        initViews();
        initEnents();
    }

    private void initViews() {
        bu_xiugai = findViewById(R.id.button3);
        te_name = findViewById(R.id.editText3);
        te_pass = findViewById(R.id.editText4);
        te_phone = findViewById(R.id.editText5);
        te_address = findViewById(R.id.editText6);
        im_return = findViewById(R.id.imageView8);

        te_name.setText(username);
        te_pass.setText(password);     //显示原密码
        te_phone.setText(mobilenum);
        te_address.setText(address);
    }

    private void initEnents(){
        bu_xiugai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username=te_name.getText().toString();
                userpass=te_pass.getText().toString();   //新密码
                mobilenum=te_phone.getText().toString();
                address=te_address.getText().toString();
                UpdateUserModel updateUserModel = new UpdateUserModel();
                updateUserModel.getUpdateUserResult(user_id,username,userpass,mobilenum,address, updateUserListener);
            }
        });
        im_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


}
